﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
namespace SZSLSystem.DAL
{
	/// <summary>
	///数据访问类 Wendy (create by HC 2013/1/14 22:28:42)
	/// </summary>
	public class Wendy
	{
		public Wendy(){}
		public bool Exists(string OID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(OID) from Wendy");
			strSql.Append(" where OID=@OID ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50)};
			parameters[0].Value = OID;

			return DbHelperOleDb.Exists(strSql.ToString(), parameters);
		}

		public int Delete(string OID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("delete from Wendy");
			strSql.Append(" where OID=@OID  ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50)};
			parameters[0].Value = OID;

			return DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
		}

		public int Add(SZSLSystem.Model.Wendy model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into Wendy( ");
			strSql.Append("[OID],[ProjectOID],[WendyNumber],[WendyName],[ModulesCount],[LayerCount],[RoomsCount],[OvergroundLayers],[UndergroundLayers])");
			strSql.Append("values (@OID,@ProjectOID,@WendyNumber,@WendyName,@ModulesCount,@LayerCount,@RoomsCount,@OvergroundLayers,@UndergroundLayers)");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50),
					new SqlParameter ("@ProjectOID",SqlDbType.VarChar,50),
					new SqlParameter ("@WendyNumber",SqlDbType.Int,4),
					new SqlParameter ("@WendyName",SqlDbType.VarChar,50),
					new SqlParameter ("@ModulesCount",SqlDbType.Int,4),
					new SqlParameter ("@LayerCount",SqlDbType.Int,4),
					new SqlParameter ("@RoomsCount",SqlDbType.Int,4),
					new SqlParameter ("@OvergroundLayers",SqlDbType.Int,4),
					new SqlParameter ("@UndergroundLayers",SqlDbType.Int,4)};
			parameters[0].Value = model.OID;
			parameters[1].Value = model.ProjectOID;
			parameters[2].Value = model.WendyNumber;
			parameters[3].Value = model.WendyName;
			parameters[4].Value = model.ModulesCount;
			parameters[5].Value = model.LayerCount;
			parameters[6].Value = model.RoomsCount;
			parameters[7].Value = model.OvergroundLayers;
			parameters[8].Value = model.UndergroundLayers;
			object obj = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
			if (obj == null)
			{
				return 1;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		public  SZSLSystem.Model.Wendy GetModel(string OID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select top 1 [OID],[ProjectOID],[WendyNumber],[WendyName],[ModulesCount],[LayerCount],[RoomsCount],[OvergroundLayers],[UndergroundLayers] from Wendy");
			strSql.Append(" where OID=@OID ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50)};
			parameters[0].Value = OID;

			SZSLSystem.Model.Wendy model=new SZSLSystem.Model.Wendy();
			DataSet ds=DbHelperOleDb.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				DataRow row = ds.Tables[0].Rows[0];
						model.OID=row["OID"].ToString();
						model.ProjectOID=row["ProjectOID"].ToString();
					if (row["WendyNumber"].ToString()!="")
					{
						model.WendyNumber=int.Parse(row["WendyNumber"].ToString());
					}
						model.WendyName=row["WendyName"].ToString();
					if (row["ModulesCount"].ToString()!="")
					{
						model.ModulesCount=int.Parse(row["ModulesCount"].ToString());
					}
					if (row["LayerCount"].ToString()!="")
					{
						model.LayerCount=int.Parse(row["LayerCount"].ToString());
					}
					if (row["RoomsCount"].ToString()!="")
					{
						model.RoomsCount=int.Parse(row["RoomsCount"].ToString());
					}
					if (row["OvergroundLayers"].ToString()!="")
					{
						model.OvergroundLayers=int.Parse(row["OvergroundLayers"].ToString());
					}
					if (row["UndergroundLayers"].ToString()!="")
					{
						model.UndergroundLayers=int.Parse(row["UndergroundLayers"].ToString());
					}

				return model;
			}
			else
			{
				return null;
			}
		}

		public int Update(SZSLSystem.Model.Wendy model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update Wendy set ");
			strSql.Append("[OID]=@OID,");
			strSql.Append("[ProjectOID]=@ProjectOID,");
			strSql.Append("[WendyNumber]=@WendyNumber,");
			strSql.Append("[WendyName]=@WendyName,");
			strSql.Append("[ModulesCount]=@ModulesCount,");
			strSql.Append("[LayerCount]=@LayerCount,");
			strSql.Append("[RoomsCount]=@RoomsCount,");
			strSql.Append("[OvergroundLayers]=@OvergroundLayers,");
			strSql.Append("[UndergroundLayers]=@UndergroundLayers");
			strSql.Append(" where OID=@OID ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50),
					new SqlParameter ("@ProjectOID",SqlDbType.VarChar,50),
					new SqlParameter ("@WendyNumber",SqlDbType.Int,4),
					new SqlParameter ("@WendyName",SqlDbType.VarChar,50),
					new SqlParameter ("@ModulesCount",SqlDbType.Int,4),
					new SqlParameter ("@LayerCount",SqlDbType.Int,4),
					new SqlParameter ("@RoomsCount",SqlDbType.Int,4),
					new SqlParameter ("@OvergroundLayers",SqlDbType.Int,4),
					new SqlParameter ("@UndergroundLayers",SqlDbType.Int,4)};
			parameters[0].Value = model.OID;
			parameters[1].Value = model.ProjectOID;
			parameters[2].Value = model.WendyNumber;
			parameters[3].Value = model.WendyName;
			parameters[4].Value = model.ModulesCount;
			parameters[5].Value = model.LayerCount;
			parameters[6].Value = model.RoomsCount;
			parameters[7].Value = model.OvergroundLayers;
			parameters[8].Value = model.UndergroundLayers;

			return DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
		}

		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select [OID],[ProjectOID],[WendyNumber],[WendyName],[ModulesCount],[LayerCount],[RoomsCount],[OvergroundLayers],[UndergroundLayers]");
			strSql.Append(" FROM Wendy ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.Query(strSql.ToString());
		}

		public DataSet GetList(int top,string strWhere,string fieldOrder)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select");
			if(top>0)
			{
				strSql.Append(" top "+top.ToString());
			}
			strSql.Append(" [OID],[ProjectOID],[WendyNumber],[WendyName],[ModulesCount],[LayerCount],[RoomsCount],[OvergroundLayers],[UndergroundLayers]");
			strSql.Append(" FROM Wendy ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
				strSql.Append(" order by "+fieldOrder);
			return DbHelperOleDb.Query(strSql.ToString());
		}

		public DataSet GetList(string showFields,string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
				strSql.Append(" select "+showFields);
			strSql.Append(" FROM Wendy ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.Query(strSql.ToString());
		}

		public object GetSingle(string fieldName,string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
				strSql.Append(" select "+fieldName);
			strSql.Append(" FROM Wendy ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.GetSingle(strSql.ToString());
		}

	}
}